﻿using System;
using System.Collections.Generic;
using LingDong.IndexEntities.Dictionary;
using LingDong.IndexReader;
using NLog;

namespace LingDong.IndexExample
{
    public class IndexReader
    {
        internal static void Run()
        {
            Logger logger = LogManager.GetCurrentClassLogger();
            logger.Debug("IndexReader starts");


            using (Index index = new Index())
            {
                //PrintAllDictionary(index, logger);

                PrintTermInfo(index, logger);
                PrintDocInfo(index, logger);
                
                foreach (string sen in index.GetParsedDocument(555).SentenceList)
                {
                    logger.Trace(sen);
                }

                Dictionary<int, InvertedListEntity> entity = index.GetInvertedList("北京");
                foreach (KeyValuePair<int, InvertedListEntity> pair in entity)
                {
                    int docid = pair.Key;
                    InvertedListEntity invertedList = pair.Value;
                    //string content = index.GetParsedDocumentByID(docid).ToString();
                    //logger.Trace(content);

                    string[] sentenceList = index.GetParsedDocument(docid).SentenceList;
                    foreach (HitListEntity hit in invertedList.HitList)
                    {
                        logger.Trace(sentenceList[hit.SentenceIndex]);
                    }
                }
                logger.Debug("IndexReader ends");
            }
        }

        private static void PrintTermInfo(Index index, Logger logger)
        {
            Console.WriteLine(index.GetTotalTermCount());
            Console.WriteLine(index.GetTermID("请"));
            Console.WriteLine(index.GetTermPOS("北京"));

            string term;
            term = index.GetTermByID(0);
            Console.WriteLine(term);
            logger.Trace("Term: 0");
            logger.Trace(term);

            term = index.GetTermByID(1000);
            Console.WriteLine(term);
            logger.Trace("Term: 1000");
            logger.Trace(term);

            term = index.GetTermByID(10000);
            Console.WriteLine(term);
            logger.Trace("Term: 10000");
            logger.Trace(term);
        }

        private static void PrintDocInfo(Index index, Logger logger)
        {
            string doc;
            Console.WriteLine(index.GetTotalDocumentCount());

            doc = index.GetOriginalDocument(0).ToString();
            logger.Trace("Ori doc: 0");
            logger.Trace(doc);

            doc = index.GetOriginalDocument(100).ToString();
            logger.Trace("Ori doc: 100");
            logger.Trace(doc);

            doc = index.GetOriginalDocument(1000).ToString();
            logger.Trace("Ori doc: 1000");
            logger.Trace(doc);

            doc = index.GetParsedDocument(1000).ToString();
            logger.Trace("Parsed doc: 1000");
            logger.Trace(doc);
        }

        private static void PrintAllDictionary(Index index, Logger logger)
        {
            int count = index.GetTotalTermCount();
            for (int i = 0; i < count; i++)
            {
                logger.Trace("ID: {0}, Term: {1}", i, index.GetTermByID(i));
            }
        }
    }
}
